package com.hw.one.transpot.fileupload.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hw.one.transpot.fileupload.constant.Constant;
import com.hw.one.transpot.fileupload.entity.ConfContent;
import com.hw.one.transpot.fileupload.entity.Shard;
import com.hw.one.transpot.fileupload.entity.ShardResult;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Class FileTransportClientHandler
 * @Author guoshenzhen
 * @Date 2018/4/1  16:59
 */
public class FileTransportClientHandler extends ChannelInboundHandlerAdapter {

    private Logger log = Logger.getLogger(this.getClass());
    private final String fileUploadPath;

    public FileTransportClientHandler(String fileUploadPath){
        this.fileUploadPath = fileUploadPath;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        excuteShard(ctx);
    }

    public void excuteShard(ChannelHandlerContext ctx) throws IOException {
        if(!ShredUtil.isConfExists()){
            ShredUtil.createShredFile(fileUploadPath);
            excuteShard(ctx);
        }else if(ShredUtil.isComplete()){
            JSONObject jsonObj = JSONObject.parseObject(ShredUtil.readFromFile());
            jsonObj.put("method", "conf");
            jsonObj.put("appId", Constant.APPID);
            jsonObj.put("appKey", Constant.APPKEY);
            ctx.writeAndFlush(jsonObj.toJSONString());
        }else {
            // 发送分片数据
            ShardResult shardResult = ShredUtil.nextShredData();
            if(null != shardResult){
                ctx.writeAndFlush(JSON.toJSONString(shardResult));
            }
        }
    }


    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 处理接收数据
        String resp = (String) msg;
        JSONObject respObject = JSON.parseObject(resp);
        if(null != respObject){
            final String method = respObject.getString("method");
            final String code = respObject.getString("code");
            if("shard".equals(method)){
                if ("111".equals(code)) {
                    JSONObject data = respObject.getJSONObject("data");
                    ShredUtil.uploadShardConf(data.toJSONString());
                }
                excuteShard(ctx);
            } else if("conf".equals(method)){
                if ("111".equals(code)){
                    ShredUtil.clearTemp();
                    this.channelInactive(ctx);
                    ctx.close();
                    return;
                }else if("002".equals(code)){
                    JSONArray datas = respObject.getJSONArray("data");
                    ShredUtil.uploadDatasConf(datas.toJSONString());
                    excuteShard(ctx);
                }
            }else if("000".equals(code)){
                super.channelInactive(ctx);
                ctx.close();
            }
        }else{
            super.channelInactive(ctx);
            ctx.close();
        }
    }
}
